//
//  LoginController.m
//  Learncloud
//
//  Created by 郭明皓 on 16/6/20.
//  Copyright © 2016年 郭明皓. All rights reserved.
//

#import "LoginController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "RegistController.h"
#import "Check.h"
@interface LoginController ()
@property (weak, nonatomic) IBOutlet UITextField *TelText;
@property (weak, nonatomic) IBOutlet UITextField *CodeText;
@property (weak, nonatomic) IBOutlet UIButton *LoginButton;
@property (weak, nonatomic) IBOutlet UIButton *RegistButton;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
}
- (IBAction)Login:(id)sender {
//    NSString *CheckTel = [Check checkMobile:_TelText.text];
//    NSString *CheckPassword = [Check checkPassword:_CodeText.text];
    if ([_TelText.text isEqualToString:@""]||_TelText.text ==nil ||[_CodeText.text isEqualToString:@""]||_CodeText.text == nil) {
        NSLog(@"密码不能为空");
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                       message:@"用户名或密码不能为空"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    else {
        AVQuery *query = [AVQuery queryWithClassName:@"Todo"];
        [query whereKey:@"Tel" equalTo:_TelText.text];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            NSArray<AVObject *> *priorityEqualsZeroTodos = objects;// 符合 priority = 0 的 Todo 数组
            NSLog(@"数组%@", objects);
            NSString *passwords = objects[objects.count - 1][@"password"];
            NSLog(@"密码%@", passwords);
            if ([_CodeText.text isEqualToString:passwords]) {
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                               message:@"登录成功"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {}];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            } else {
                NSLog(@"错误");
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                               message:@"登录失败"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {}];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
            
        }];

    }
    
    
//    AVQuery *query = [AVQuery queryWithClassName:@"Todo"];
//    [query getObjectInBackgroundWithId:@"57676c737f57850054888d1b" block:^(AVObject *object, NSError *error) {
//        // object 就是 id 为 558e20cbe4b060308e3eb36c 的 Todo 对象实例
////        int priority = [[object objectForKey:@"priority"] intValue];
//        NSString *Tel = [object objectForKey:@"Tel"];
//        NSString *Password = [object objectForKey:@"password"];
//        NSLog(@"手机%@, 密码%@", Tel, Password);
//        if ([_TelText.text isEqualToString:Tel] && [_CodeText.text isEqualToString:Password]) {
//            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"手机"
//                                                                           message:@"登录成功"
//                                                                    preferredStyle:UIAlertControllerStyleAlert];
//            
//            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
//                                                                  handler:^(UIAlertAction * action) {}];
//            
//            [alert addAction:defaultAction];
//            [self presentViewController:alert animated:YES completion:nil];
//        } else {
//            NSLog(@"错误");
//        }
//
//        // 获取三个特殊属性
////        NSString *objectId = object.objectId;
////        NSDate *updatedAt = object.updatedAt;
////        NSDate *createdAt = object.createdAt;
//    }];
    
}
- (IBAction)Regist:(id)sender {
    RegistController *regist = [[RegistController alloc]init];
    [self.navigationController pushViewController:regist animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
